<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { queryRigth2 } from '@/api/business/largeScreen'

const animationDuration = 6000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart('03')
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(zhlb) {
      this.chart = echarts.init(this.$el, 'macarons')

      queryRigth2({ zhlb: zhlb }).then(res => {
        console.log('queryRigth2:{}', res)
        this.chart.setOption({
          title: {
            text: '每月复核人数',
            x: 'center',
            y: 'center',
            textStyle: {
              color: '#FFFFFF',
              fontWeight: 'normal',
              fontSize: 12
            }
          },
          tooltip: {
            show: true,
            trigger: 'item',
            formatter: '{b}: {c} ({d}%)'
          },
          series: [{
            type: 'pie',
            selectedMode: 'single',
            radius: ['40%', '68%'],
            color: ['#86D560', '#AF89D6', '#59ADF3', '#FF999A', '#FFCC67', '#FFF68F'],

            label: {
              normal: {
                position: 'inner',
                formatter: '{d}%',

                textStyle: {
                  color: '#fff',
                  fontWeight: 'bold',
                  fontSize: 12
                }
              }
            },
            labelLine: {
              normal: {
                show: false
              }
            },
            data: res.pieArray
          }, {
            type: 'pie',
            radius: ['68%', '100%'],
            itemStyle: {
              normal: {
                color: '#4682B4'
              },
              emphasis: {
                color: '#4682B4'
              }
            },
            label: {
              normal: {
                position: 'inner',
                formatter: '{c}人',
                textStyle: {
                  color: '#FEC201',
                  fontWeight: 'bold',
                  fontSize: 12
                }
              }
            },
            data: res.pieArray
          }]
        })
      })
    }
  }
}
</script>
